import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
import {
    getCustomerList,
    existIdcard,
    createCustomer,
    getCustomerDetails,
    createCustomerRelation,
    updateCustomerDetails,
    getCustomerRelation,
    deleteCustomer,
    getTrackList,
    deleteCustomerRelation,
    addCustomerTrack,
    getApplyCustomerList,
} from '../services/api';

export default {
    namespace: 'customer',

    state: {
        customerList: [],
        customerPage: {},
        customerLoading: false,
        customerResult: {},
        customerDetailsObj: {},
        customerRelationList: [],
        customerTrackList: [],
        applyCustomerList: null,
    },

    effects: {
        //加载客户列表
        *fetchCustomer({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(getCustomerList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    customerList: response.data.list,
                    customerPage: response.data.page,
                }
            });
            if (callback) callback();
        },

        //加载客户列表
        *getApplyCustomerList({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(getApplyCustomerList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    applyCustomerList: response.data.list,
                    customerPage: response.data.page,
                }
            });
            if (callback) callback();
        },

        //加载客户关系人列表
        *fetchCustomerRelation({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(getCustomerRelation, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    customerRelationList: response.data,
                }
            });
            if (callback) callback();
        },

        //客户新增
        *createCustomer({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(createCustomer, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    customerResult: response.result,
                }
            });
            if (callback) callback();
        },

        //删除客户
        *deleteCustomer({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(deleteCustomer, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            if (response.result.code == "200") {
                message.success('删除成功');

            } else {
                message.error(response.result.message);
            }
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                },
            });
            if (callback) callback();
        },

        //删除客户关系人
        *deleteCustomerRelation({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(deleteCustomerRelation, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            if (response.result.code == '200') {
                message.success("删除成功")
            } else {
                message.error(response.result.message)
            }
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                },
            });
            if (callback) callback();
        },

        //客户关系人新增
        *createCustomerRelation({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(createCustomerRelation, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    customerResult: response.result,
                }
            });
            if (callback) callback();
        },

        //客户详情
        *getCustomerDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(getCustomerDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    customerDetailsObj: response.data,
                }
            });
            if (callback) callback();
        },

        //获取销售跟踪
        *getTrackList({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(getTrackList, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    customerTrackList: response.data,
                }
            });
            if (callback) callback();
        },

        //修改客户详情
        *updateCustomerDetails({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            const response = yield call(updateCustomerDetails, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            if (response.result.code == 200) {
                message.success("提交成功")
            } else {
                message.error(response.result.message)
            }
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    customerResult: response.result,
                },
            });
            if (callback) callback(response.result.code);
        },

        //新增客户销售跟踪
        *addCustomerTrack({ payload, callback }, { call, put }) {
            yield put({
                type: 'save',
                payload: {
                    customerLoading: true,
                },
            });
            const response = yield call(addCustomerTrack, payload);
            if (!isResponerCode(response)) {
                yield put({
                    type: 'save',
                    payload: {
                        customerLoading: false,
                    },
                });
                return false
            }
            yield put({
                type: 'save',
                payload: {
                    customerLoading: false,
                },
            });
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    customerResult: response.result
                }
            });
            if (callback) callback();
        },
        //检查身份证号码是否存在
        *existIdcard({ payload, callback }, { call, put }) {
            const response = yield call(existIdcard, payload);
            yield put({
                type: 'save',
                payload: {
                    data: response.data,
                    customerResult: response.result,
                }
            });
            if (callback) callback();
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};
